package com.wtp.wallpaper.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtp.wallpaper.annotation.ReplaceURL;
import com.wtp.wallpaper.pojo.dto.ClassifyDTO;
import com.wtp.wallpaper.pojo.entity.Classify;
import com.wtp.wallpaper.pojo.query.PageQuery;
import com.wtp.wallpaper.pojo.vo.ClassifyVO;
import com.wtp.wallpaper.pojo.result.Result;
import com.wtp.wallpaper.service.ClassifyService;
import com.wtp.wallpaper.utils.PojoConvertUtil;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//@Api("分类相关接口")
@RestController("adminClassifyController")
@Slf4j
@RequestMapping("/admin/classify")
public class ClassifyController {

    @Autowired
    private ClassifyService classifyService;

    //获取所有分类
    @GetMapping("/classifyList")
    public Result getClassifyList(){
        return Result.success(PojoConvertUtil.convert(classifyService.list(),ClassifyVO.class));
    }

    @ReplaceURL
    @GetMapping("/classifyPage")
    public Result queryClassifyPage(@RequestParam(defaultValue = "1") int pageNum,
                               @RequestParam(defaultValue = "5") int pageSize){
        Page<Classify> page = classifyService.page(new PageQuery(pageNum, pageSize).toMpPage("sort",true));
        return Result.success(PojoConvertUtil.convert(page, ClassifyVO.class));
    }

    @PostMapping("/removeByIds")
    public Result removeByIds(ClassifyDTO classifyDTO){
        log.info("传来ids:{}",classifyDTO);
        String s = classifyService.removeClassifyByIds(classifyDTO);
        //pictureService.removeByIds();
        return StringUtil.isNullOrEmpty(s) ? Result.success("全部删除成功") : Result.error(s + "\n其余删除成功");
    }

    @PostMapping("/addOne")
    public Result addOneClassify(ClassifyDTO classifyDTO){
        log.info("传来数据:{}",classifyDTO);
        classifyService.addOne(classifyDTO);
        return Result.success();

    }

    @PostMapping("/modifyOne")
    public Result modifyOnePicture(ClassifyDTO classifyDTO){
        log.info("传来数据:{}",classifyDTO);
        classifyDTO.setPicurl(classifyDTO.getUrl());
        classifyService.update(classifyDTO);
        return Result.success();
    }

}
